<?php
/**
 * 
 * @author	ronnie  (comdeng@gmail.com)
 * @link		http://fillphp.com/
 * @date		2014-5-2 下午9:44:18
 * @file		FillView.php
 * @description 
 */
namespace fillphp\view;

use fillphp\helper\Helper;
require_once __DIR__.'/View.php';
require_once dirname(__DIR__).'/helper/Helper.php';

// 需要使用hook机制实现对填充的处理
require_once FR_ROOT.'/hook/Hook.php';

class FillView extends View
{
	private $cacheDir;
	
	private $wrappers = array();
	
	/**
	 * 
	 * @param \fillphp\page\Request $request
	 */
	function __construct($request)
	{
		parent::__construct($request);
		
		if (defined('TMP_ROOT')) {
			$this->cacheDir = TMP_ROOT.'/fillview/';
		}
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \fillphp\view\IView::init()
	 */
	function init(array $conf = array())
	{
		parent::init($conf);
		
		if (isset($conf['cacheDir'])) {
			$this->cacheDir = $conf['cacheDir'];
		}
	}
	
	/**
	 * 增加内容封装器，在调用display渲染页面内容后会调用这些回调函数处理内容
	 * @param \Callable $callable
	 */
	function addWrapper($callable)
	{
		$this->wrappers[] = $callable;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \fillphp\view\View::display()
	 */
	function display($path)
	{
		if ($this->curBlankName) {
			throw new Exception('\fillphp\view\fillview\blankNotClosed blank:'.$this->curBlankName);
		}
		ob_start();
		require $path;
		$content = ob_get_clean();
		
		foreach($this->wrappers as $wrapper) {
			$content = call_user_func_array($wrapper, array($content));
		}
		return $content;
	}
	
	function fileCache()
	{
		$helper = self::__call('fileCache', array());
		$helper->setTmpDir($this->cacheDir);
		return $helper;
	}
}
